package com.wjs.gulimall.gulimallorder.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.wjs.gulimall.gulimallorder.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000119612814";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCJsQTbAmEkKSnCD8rguPGxxsz5qSaaCpTQgXAGVAxlahn9r7uZxtePBva4eX5Egk4mRs980HZ8JFHLoN/Lzchsc4H/mPN+1GiiecdICM9HAtT7Iq51PlizPWolxlli0gpvLazpICOVKrGN4eXF3XLmRbBY+Hg+To/2QbVULrL2XWIAFjCJFoZ9ZjUNpWha4dHq+/ICUMT8n7b1TzUUMpXhZuazXDG10eOCVuZ+x+ZvWhQ3nbKpDv9IsAlIe+iWM5LQbK/HURBr5Vr6K0Rqci5XWDq48fzscvq4RfgQC9ZL6roQquVBXiEYdWvrLMsqYXWa2/Ln4Xuq5lSQAJXdMoBHAgMBAAECggEAT+kXi0+XNxhYrtLhiIhtZSKehWqhB5eKhHQKovGWfCkWyn953iZjphOBjtCb8oC5gEdqVeLaUPP3MLm+CJb2cdKbqM6IypvV2i3Y9Tp/qZPALbwUCeFUMzo6Z4/1AoAqJBobFvVbEVBE2bY31xzNeU0KqqgRiP+pV18NML0ligsI4niqSqtseV45sgs8mzeiLcV9LyrZkf12EQaqHyqeJOHyw7Ybi++qfXzkek4xlsuLbxH9cJSzoq7jTWbhpnEtIqedWwPVAtbtcw8xQXmjtJz3gZf7QWJKKVlwHIPlvpd2aFmzrGEvAtpox8VlwHtl0/TNBcOpZcnU24Fs8Sj4MQKBgQDzksIgVRQJ1n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DxxWkBf3fSK+0ZH5F4JyAir2++oYuDLU5n7f+iQXdY0UscT9JhK8JlRWxGUKJzZcIG1UpTYE4oH+xvErj0cZv618jRsgOvxSJxcrh9t6UOeNUgKrGTcTMZv37fgNEoFGGhQ9Xbhtf3QqSZZqqfpCJul2rytovWyDn07QH4BVkSZ55vBu9y4M7vsxEwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://wqct94.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
